﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Xml;

/// <summary>
/// 下载淘宝用户数据
/// </summary>
public class TB_UserInfo
{
    public TB_UserInfo()
    {        
    }
    public BS2.BSTaobaoAPI.BSTaobaoUser GetTBUserInfo(string username, string password, string appkey, string certcode, string sessionId, string buyer_name)
    {
        string[,] param = new string[,] {{ "sip_apiname", "taobao.user.get" },{"nick",buyer_name},
                    {"fields","nick,sex,buyer_credit,seller_credit,location.city,location.state,location.country,created,last_visit,real_name,id_card"},
                    { "v", "1.0" }, { "sip_timestamp", System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") }, { "sip_format", "xml" },{"sip_appkey",appkey} };
        BS2.BSTaobaoAPI.TaobaoAPI.setAPPID(appkey);
        BS2.BSTaobaoAPI.TaobaoAPI.setCERTCODE(certcode);
        BS2.BSTaobaoAPI.TaobaoAPI.setLoginPro(username, password);
        BS2.BSTaobaoAPI.TaobaoAPI.setSessionID(sessionId);
        XmlDocument xml = BS2.BSTaobaoAPI.TaobaoAPI.getXmlFromTaobao(param);
        if (xml == null || xml.OuterXml.Contains("error_rsp"))
            return null;
        return GetTBUserInfo(xml);
    }
    BS2.BSTaobaoAPI.BSTaobaoUser GetTBUserInfo(XmlDocument xml)
    {
        BS2.BSTaobaoAPI.BSTaobaoUser t = new BS2.BSTaobaoAPI.BSTaobaoUser();
        foreach (XmlNode node in xml.SelectNodes("/rsp/user"))
        {
            if (node.SelectNodes("nick").Item(0) != null)
            {
                t._nick = node.SelectNodes("nick").Item(0).InnerText;
            }
            if (node.SelectNodes("sex").Item(0) != null)
            {
                t._sex = node.SelectNodes("sex").Item(0).InnerText;
            }
            if (node.SelectNodes("buyer_credit").Item(0) != null)
            {
                t._buyer_credit = node.SelectNodes("buyer_credit").Item(0).InnerText;
            }
            if (node.SelectNodes("seller_credit").Item(0) != null)
            {
                t._seller_credit = node.SelectNodes("seller_credit").Item(0).InnerText;
            }
            if (node.SelectNodes("location/city").Item(0) != null)
            {
                t._location_city = node.SelectNodes("location/city").Item(0).InnerText;
            }
            if (node.SelectNodes("location/state").Item(0) != null)
            {
                t._location_state = node.SelectNodes("location/state").Item(0).InnerText;
            }
            if (node.SelectNodes("location/country").Item(0) != null)
            {
                t._location_country = node.SelectNodes("location/country").Item(0).InnerText;
            }
            if (node.SelectNodes("created").Item(0) != null)
            {
                t._created = node.SelectNodes("created").Item(0).InnerText;
            }
            if (node.SelectNodes("last_visit").Item(0) != null)
            {
                t._last_visit = node.SelectNodes("last_visit").Item(0).InnerText;
            }
            if (node.SelectNodes("location/zip").Item(0) != null)
            {
                t._location_zip = node.SelectNodes("location/zip").Item(0).InnerText;
            }
            if (node.SelectNodes("location/address").Item(0) != null)
            {
                t._location_address = node.SelectNodes("location/address").Item(0).InnerText;
            }
            if (node.SelectNodes("real_name").Item(0) != null)
            {
                t._real_name = node.SelectNodes("real_name").Item(0).InnerText;
            }
            if (node.SelectNodes("id_card").Item(0) != null)
            {
                t._id_card = node.SelectNodes("id_card").Item(0).InnerText;
            }
            if (node.SelectNodes("phone").Item(0) != null)
            {
                t._phone = node.SelectNodes("phone").Item(0).InnerText;
            }
            if (node.SelectNodes("mobile").Item(0) != null)
            {
                t._mobile = node.SelectNodes("mobile").Item(0).InnerText;
            }
            if (node.SelectNodes("email").Item(0) != null)
            {
                t._email = node.SelectNodes("email").Item(0).InnerText;
            }
            if (node.SelectNodes("birthday").Item(0) != null)
            {
                t._birthday = node.SelectNodes("birthday").Item(0).InnerText;
            }
            if (node.SelectNodes("type").Item(0) != null)
            {
                t._type = node.SelectNodes("type").Item(0).InnerText;
            }
            if (node.SelectNodes("has_more_pic").Item(0) != null)
            {
                t._has_more_pic = node.SelectNodes("has_more_pic").Item(0).InnerText;
            }
            if (node.SelectNodes("shop_id").Item(0) != null)
            {
                t._shop_id = node.SelectNodes("shop_id").Item(0).InnerText;
            }
            if (node.SelectNodes("itemimg_num").Item(0) != null)
            {
                t._itemimg_num = node.SelectNodes("itemimg_num").Item(0).InnerText;
            }
            if (node.SelectNodes("itemimg_size").Item(0) != null)
            {
                t._itemimg_size = node.SelectNodes("itemimg_size").Item(0).InnerText;
            }
            if (node.SelectNodes("propsimg_num").Item(0) != null)
            {
                t._propsimg_num = node.SelectNodes("propsimg_num").Item(0).InnerText;
            }
            if (node.SelectNodes("propsimg_size").Item(0) != null)
            {
                t._propsimg_size = node.SelectNodes("propsimg_size").Item(0).InnerText;
            }
            if (node.SelectNodes("auto_repost").Item(0) != null)
            {
                t._auto_repost = node.SelectNodes("auto_repost").Item(0).InnerText;
            }
        }
        return t;
    }
}
